package com.naiterui.ehp.bp.support.exception;

import static com.naiterui.ehp.bp.support.message.MessageContext.message;

import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;

import io.swagger.annotations.ApiModelProperty;

/**
 * 通用数据响应对象
 * 用于service向controller的请求响应
 */
@Data
public class ResponseVO<T> {
    
    /**
     * 状态码
     * 0：表示正常
     * 其余数值表示出现错误，该值对应ExceptionCode中定义的异常码
     */
    @ApiModelProperty("状态码，0表示正常")
    private int code;

    /**
     * 信息内容
     */
    @ApiModelProperty("错误信息")
    private String msg;

    /**
     * 数据信息
     */
    @ApiModelProperty("数据信息")
    private T data;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timestamp;

    public ResponseVO() {
        this(BaseExceptionCodes.SUCCESS, null);
    }

    public ResponseVO(int code) {
        this(code, null);
    }

    public ResponseVO(int code, T data) {
        this(code, message(code), data);
    }

    public ResponseVO(int code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
        this.timestamp = LocalDateTime.now();
    }

}
